package com.liming.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.liming.item.CartGoodItems;
import com.liming.model.CartGoods;
import com.liming.model.User;
import com.liming.service.CartGoodsService;
import com.sun.org.apache.xpath.internal.operations.Bool;

@WebServlet("/CartGoods")
public class CartGoodsServlet extends HttpServlet {

	CartGoodsService cg = new CartGoodsService();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String method = (String)request.getParameter("method");
		if(method.equals("changeQuantity"))
		{
			changeQuantity(request,response);
		}
		else if (method.equals("initializeCart")) {
			initializeCart(request,response);
		}
		else if (method.equals("putInCart")) {
			putInCart(request,response);
		}
		else if (method.equals("getAllGoods")) {
			getAllGoods(request,response);
		}
	}

	private void getAllGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		User user = (User)request.getSession().getAttribute("user");
		
		if (user == null) {
			System.out.println("未登录");
			response.sendRedirect(request.getContextPath()+"/login.jsp");
			return ;
		}
		
		int userid = user.getUserid();
		
		List<CartGoodItems> goods= cg.getAllGoods(userid);
		
		String allgoods = JSON.toJSONString(goods);
		
		System.out.println(allgoods);
		
		response.getWriter().write(allgoods);
	}

	private void putInCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("utf-8");
		
		User user = (User)request.getSession().getAttribute("user");
		int userid = user.getUserid();
		int goodsid = Integer.parseInt(request.getParameter("goodsid"));
		int goodsnum = Integer.parseInt(request.getParameter("goodsnum"));
		String goodsdetial = (String)request.getParameter("goodsdetial");
		
		CartGoods cartGoods = new CartGoods(userid, goodsid, goodsnum);
		
		Boolean flag = cg.putInCart(cartGoods);
		if(flag) {
			System.out.println("数据插入成功");
		}
		else{
			System.out.println("数据插入成功");
		}
		
	}

	private void initializeCart(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		
		
		User user = (User)request.getSession().getAttribute("user");
		int userid = user.getUserid();
		List<CartGoods> goods= cg.initializeCart(userid);
		
		request.setAttribute("cartGoods", goods);
		//显示界面
		request.getRequestDispatcher("#.jsp").forward(request, response);
	}
	
	

	private void changeQuantity(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("utf-8");
		
		User user = (User)request.getSession().getAttribute("user");
		int userid = user.getUserid();
		int goodsid = Integer.parseInt( request.getParameter("goodsid"));
		int goodsnum = Integer.parseInt(request.getParameter("goodsnum"));
		
		CartGoods cartGoods = new CartGoods(userid,goodsid,goodsnum);
		
		int ans = cg.changeQuantity(cartGoods);
		
		System.out.println(ans==1?"修改成功":"修改失败");
		
		response.sendRedirect(request.getContextPath()+"/index.jsp");
	}
}
